package com.llt.utils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.util.DigestUtils;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Utils {
    private static final long  EXPIRE_TIME = 15 * 60 * 1000;//默认15分钟
    //私钥
    private static final String TOKEN_SECRET = "199903251015llt";
    /**
     *
     * @param content 加密字符串
     * @return
     */
    public static String getMd5_2(String content){
        String s = DigestUtils.md5DigestAsHex(content.getBytes());
        String s1 = DigestUtils.md5DigestAsHex(s.getBytes());
        return s1;
    }

    /**
     * token 加密
     * @param username
     *@param expireTime 有效时间
     * @return
     */
    public static String getToken(String username ,Long... expireTime){
            long  currentTimes  = expireTime[0]!=null? expireTime[0]:EXPIRE_TIME;
        //设置过期时间
        Date date = new Date(System.currentTimeMillis() + currentTimes);
        //加密算法私钥


        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
        //设置头部信息
        Map<String, Object> data = new HashMap<>(2);
        data.put("Type","Jwt");
        data.put("alg","HMAC256");
        //开始创建token
        String username1 = JWT.create()
                .withHeader(data)
                .withExpiresAt(date)
                .withClaim("username", username)
                .sign(algorithm);
        return username1;
    }

    /**
     *解析token
     * @param token
     * @return 返回名字
     */
    public static String verifyToken(String token) {
        //加密算法私钥
        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
        //获取校验实例
        JWTVerifier verifier = JWT.require(algorithm).build();
        DecodedJWT   verify;
        try {
            verify   = verifier.verify(token);
        }catch (NullPointerException e){
            System.out.println("com.llt.utils.Utils 工具异常报错70行 token 是空");
            throw new JWTDecodeException("token 是空");
        }
        Map<String, Claim> claims = verify.getClaims();
        Claim username = claims.get("username");
        return   username.asString();

    }

    /**
     * //生成6位不重复的验证码
     * @return 6位不重复的验证码
     */
    public static String codeGet(){
        char [] codeSequence={'A','B','C','D','E','F','G','H','I','J',
                'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
                '1','2','3','4','5','6','7','8','9'};
        Random random =new Random();
        StringBuilder sb=new StringBuilder();//动态字符串，String创建的字符串不能修改
        int count=0;//计数器确定产生的是四位验证码
        while(true){
            //随机产生一个下标，通过下标取出字符数组对应的字符
            char c=codeSequence[random.nextInt(codeSequence.length)];
            //假设取出来的字符在动态字符串中不存在，代表没有重复
            if (sb.indexOf(c+"")==-1) {
                sb.append(c);//追加到动态字符串中
                count++;
                if (count==6) {
                    break;
                }
            }
        }

        return sb.toString();

    }
    public static EmailUtil sendMailFactory(){
        return new EmailUtil();
    }
}
